function ScatterPeaks()
filename = 'PeaksJoinedCWDJDSJF.mat';
load(filename);
f=[];
dataset = SelectPeaksFromDatatable(header, datatable);
col = strcmp(header, 'Condition');
for i = 1:length(dataset)
    scData = dataset(i).Result;
    B = unique(scData(:,col));
    f(end+1) = figure; hold on;
    for b = 1:length(B);
        subset = scData(strcmp(scData(:,col), B{b}),:);
        X = [subset{:, strcmp(header, 'Latency (ms)')}];
        Y = [subset{:, strcmp(header, 'Value')}];
        T = subset(:, strcmp(header, 'Channel'));
        
        if ~isempty(findstr(B{b},'Sphere')), C = 'b';
        elseif ~isempty(findstr(B{b},' House ')), C = 'g';
        elseif ~isempty(findstr(B{b},' Face ')), C = 'r';
        end
        
        if ~isempty(findstr(B{b},'Ambiguous')), markertype = 'o';
        elseif ~isempty(findstr(B{b},'Unambiguous')), markertype = '^';
        end

        SC = scatter(X,Y, [], C, markertype, 'filled');
        set(SC, 'DisplayName', B{b});
        
        for p = 1:length(X)
            text(X(p)+20,Y(p),T{p},'HorizontalAlignment','left','VerticalAlignment','middle','Interpreter','none');
        end
    end
    xlabel('Latency (ms)');
    ylabel('Value');
    title([filename ':  ' dataset(i).Name]);
    legend('show');
end

saveMultipleFigures(f, [], false);
save('dataset.mat', 'header','dataset','-v7.3');
end




function [ dataset ] = SelectPeaksFromDatatable(header, datatable)
%SELECTPEAKSFROMDATATABLE

%% 'Most Negative Alpha Between 0-2s'
dataset(1).Name = 'Stim 0s..2s, most neg. alpha';
dataset(1).Query.Where.Latency.min = 0;
dataset(1).Query.Where.Latency.max = 2000;
dataset(1).Query.Where.Frequency = [7 13];
dataset(1).Query.Where.Type = '-';
dataset(1).Query.Where.Condition.range = { ...
    'FaceHouse Unambiguous Face Stimulus', ...
    'FaceHouse Unambiguous House Stimulus', ...
    'Sphere Unambiguous Stimulus'};
dataset(1).Query.Where.Channel.range = {'OCC_D', 'OCC_V', 'IPS_I', 'IPS_S'};
dataset(1).Query.GroupBy = {'Condition', 'Channel'};
dataset(1).Query.Question = 'Min';
dataset(1).Result = SelectFromData(header, datatable, dataset(1).Query);

%% 'Most Positive High Gamma Between 0-2s'
dataset(2).Name = 'Stim 0s..2s, most pos. high gamma';
dataset(2).Query.Where.Latency.min = 0;
dataset(2).Query.Where.Latency.max = 2000;
dataset(2).Query.Where.Frequency = [70 130];
dataset(2).Query.Where.Type = '+';
dataset(2).Query.Where.Condition.range = { ...
    'FaceHouse Unambiguous Face Stimulus', ...
    'FaceHouse Unambiguous House Stimulus', ...
    'Sphere Unambiguous Stimulus'};
dataset(2).Query.Where.Channel.range = {'OCC_D', 'OCC_V', 'IPS_I', 'IPS_S'};
dataset(2).Query.GroupBy = {'Condition', 'Channel'};
dataset(2).Query.Question = 'Max';
dataset(2).Result = SelectFromData(header, datatable, dataset(2).Query);

%% 'Most Negative Alpha Between 0-2s'
dataset(3).Name = 'Unamb/Amb Resp -1s..1s, most neg. alpha';
dataset(3).Query.Where.Latency.min = -1000;
dataset(3).Query.Where.Latency.max = 1000;
dataset(3).Query.Where.Frequency = [7 13];
dataset(3).Query.Where.Type = '-';
dataset(3).Query.Where.Condition.range = { ...
    'FaceHouse Ambiguous Face Response', ...
    'FaceHouse Ambiguous House Response', ...
    'FaceHouse Unambiguous Face Response', ...
    'FaceHouse Unambiguous House Response', ...
    'Sphere Ambiguous Response', ...
    'Sphere Unambiguous Response'};
dataset(3).Query.Where.Channel.range = {'OCC_D', 'OCC_V', 'IPS_I', 'IPS_S'};
dataset(3).Query.GroupBy = {'Condition', 'Channel'};
dataset(3).Query.Question = 'Min';
dataset(3).Result = SelectFromData(header, datatable, dataset(3).Query);

%% 'Most Positive High Gamma Between 0-2s'
dataset(4).Name = 'Unamb/Amb Resp -1s..1s, most pos. high gamma';
dataset(4).Query.Where.Latency.min = -1000;
dataset(4).Query.Where.Latency.max = 1000;
dataset(4).Query.Where.Frequency = [70 130];
dataset(4).Query.Where.Type = '+';
dataset(4).Query.Where.Condition.range = { ...
    'FaceHouse Ambiguous Face Response', ...
    'FaceHouse Ambiguous House Response', ...
    'FaceHouse Unambiguous Face Response', ...
    'FaceHouse Unambiguous House Response', ...
    'Sphere Ambiguous Response', ...
    'Sphere Unambiguous Response'};
dataset(4).Query.Where.Channel.range = {'OCC_D', 'OCC_V', 'IPS_I', 'IPS_S'};
dataset(4).Query.GroupBy = {'Condition', 'Channel'};
dataset(4).Query.Question = 'Max';
dataset(4).Result = SelectFromData(header, datatable, dataset(4).Query);


end


function datatable = SelectFromData(header, datatable, select)
%% Filter the data
tfSelect = true(size(datatable,1),1);
% Select the latency
if any(strcmp(fieldnames(select.Where), 'Latency'))
    tfSelect = tfSelect & ([datatable{:,strcmp(header,'Latency (ms)')}]>select.Where.Latency.min)' & ([datatable{:,strcmp(header,'Latency (ms)')}]<select.Where.Latency.max)';
end
% Select the frequency bin
if any(strcmp(fieldnames(select.Where), 'Frequency'))
    tfSelect = tfSelect & ([datatable{:,strcmp(header,'FrequencyMin')}]==select.Where.Frequency(1))';
end
% Select the peak type
if any(strcmp(fieldnames(select.Where), 'Type'))
    tfSelect = tfSelect & strcmp(datatable(:,strcmp(header,'Type')), select.Where.Type);
end
% Select the conditions
if any(strcmp(fieldnames(select.Where), 'Condition'))
    tfCond = false(size(tfSelect));
    for i = 1:length(select.Where.Condition.range)
        tfCond = tfCond | strcmp(datatable(:,strcmp(header,'Condition')), select.Where.Condition.range{i});
    end
    tfSelect = tfSelect & tfCond;
end
% Select the channels
if any(strcmp(fieldnames(select.Where), 'Channel'))
    tfCh = false(size(tfSelect));
    for i = 1:length(select.Where.Channel.range)
        tfCh = tfCh | strcmp(datatable(:,strcmp(header,'Channel')), select.Where.Channel.range{i});
    end
    tfSelect = tfSelect & tfCh;
end


%% Execute the filter
datatable = datatable(tfSelect,:);

%% Group the data
groups = GetGroupedByData(header, datatable, select.GroupBy);
switch select.Question
    case 'Max'
        datatable = SelectMaxFromGroups(header, 'Value', groups);
    case 'Min'
        datatable = SelectMinFromGroups(header, 'Value', groups);
end


end

function groupData = GetGroupedByData(header, datatable, groupbyToDo, groupData)
if length(groupbyToDo) < 1, error(), end;
if ~exist('groupData','var'), groupData = []; end;

col = find(strcmp(header, groupbyToDo{1}));
B = unique(datatable(:,col));
for i = 1:length(B)
%     disp(B{i});
    if length(groupbyToDo) == 1
        groupData(end+1).Data = datatable(strcmp(datatable(:,col), B{i}),:);
    else
        groupData = GetGroupedByData(header, datatable(strcmp(datatable(:,col), B{i}),:), groupbyToDo(2:end), groupData);
    end
end

end

function datatable = SelectMaxFromGroups(header, field, groups)
datatable = cell(length(groups), length(header));
col = strcmp(header, field);
for i = 1:length(groups)
    [Y I] = max([groups(i).Data{:,col}]);
    datatable(i,:) = groups(i).Data(I,:);
end
end

function datatable = SelectMinFromGroups(header, field, groups)
datatable = cell(length(groups), length(header));
col = strcmp(header, field);
for i = 1:length(groups)
    [Y I] = min([groups(i).Data{:,col}]);
    datatable(i,:) = groups(i).Data(I,:);
end
end